package com.seeingtv.service.login;

import com.seeingtv.common.async.AsyncFactory;
import com.seeingtv.common.async.AsyncManager;
import com.seeingtv.common.constant.Constants;
import com.seeingtv.common.constant.ShiroConstants;
import com.seeingtv.common.constant.UserConstants;
import com.seeingtv.common.constant.UserStatus;
import com.seeingtv.common.exception.user.CaptchaException;
import com.seeingtv.common.exception.user.UserBlockedException;
import com.seeingtv.common.exception.user.UserNotExistsException;
import com.seeingtv.common.exception.user.UserPasswordNotMatchException;
import com.seeingtv.common.utils.MessageUtils;
import com.seeingtv.common.utils.ServletUtils;
import com.seeingtv.common.utils.SystemLogUtils;
import com.seeingtv.model.User;
import com.seeingtv.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author hubert
 * @date 2018/8/8
 * @description SystemServiceImpl
 */
@Component
public class LoginService {

	@Autowired
	private UserService userService;

	@Autowired
	private PasswordService passwordService;

	public User login(String username, String password) {
		// 验证码校验
		if (!StringUtils.isEmpty(ServletUtils.getRequest().getAttribute(ShiroConstants.CURRENT_CAPTCHA))) {
			SystemLogUtils.log(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"));
			throw new CaptchaException();
		}
		// 用户名或密码为空 错误
		if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
			SystemLogUtils.log(username, Constants.LOGIN_FAIL, MessageUtils.message("not.null"));
			throw new UserNotExistsException();
		}
		// 密码如果不在指定范围内 错误
		if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
				|| password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
			SystemLogUtils.log(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match"));
			throw new UserPasswordNotMatchException();
		}

		// 用户名不在指定范围内 错误
		if (username.length() < UserConstants.USERNAME_MIN_LENGTH
				|| username.length() > UserConstants.USERNAME_MAX_LENGTH) {
			SystemLogUtils.log(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match"));
			throw new UserPasswordNotMatchException();
		}

		// 查询用户信息
		User user = userService.selectUserByLoginName(username);

		if (user == null || UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
			//SystemLogUtils.log(username, Constants.LOGIN_FAIL, MessageUtils.message("user.not.exists"));
			AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.not.exists")));
			throw new UserNotExistsException();
		}

		//passwordService.validate(user, password);

		if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
			SystemLogUtils.log(username, Constants.LOGIN_FAIL, MessageUtils.message("user.blocked", user.getRemark()));
			throw new UserBlockedException(user.getRemark());
		}
		SystemLogUtils.log(username, Constants.LOGIN_SUCCESS, "user.login.success");
		recordLoginInfo(user);
		return user;
	}


	/**
	 * 记录登录信息
	 */
	public void recordLoginInfo(User user) {
//		user.setLoginIp(ShiroUtils.getIp());
//		user.setLoginDate(DateUtils.getNowDate());
//		userService.updateUserInfo(user);
	}



}
